"""
统计任务年增长
"""
from xjlibrary.configread.mtoml.ReadToml import MToml
from xjlibrary.tools.excelpara import OpenpyxlUtils

from logicaltool.BaseTools.file_tools import FileTools


class CountGrowth(FileTools):
    def __init__(self):
        super().__init__()

    def read_config(self):
        self.dic = MToml(self.config_file).read_file_remove_bom().get_dicts()

    def deal_mysql_select(self, rows):
        for provider, srv_product, provider_popular in rows:
            # if srv_product.find("智图") > -1:
            #     self.inputobj1.add((provider, provider_popular))
            self.inputobj1.add((provider, provider_popular))

    def select_count(self, provider, enddate, stratdate, provider_popular):
        now_count_metadata = ""
        old_count_metadata = ""
        now_count_fulltext = ""
        old_count_fulltext = ""
        now_count_ref = ""
        old_count_ref = ""
        now_count_cover = ""
        old_count_cover = ""
        # count_metadata 题录
        # count_fulltext 全文量
        # count_ref 引文量
        # count_cover 封面量
        sql = "select provider,count_metadata,count_fulltext,count_ref,count_cover,update_date from updating where provider = '{}' and update_date < '{}' order by update_date desc".format(
            provider, enddate)
        self.set_sql(sql)
        rows = self.mysql_select()
        for row in rows:
            now_count_metadata = row[1]
            now_count_fulltext = row[2]
            now_count_ref = row[3]
            now_count_cover = row[4]
            break
        sql = "select provider,count_metadata,count_fulltext,count_ref,count_cover,update_date from updating where provider = '{}' and update_date<'{}' order by update_date desc".format(
            provider, stratdate)
        self.set_sql(sql)
        rows = self.mysql_select()
        for row in rows:
            old_count_metadata = row[1]
            old_count_fulltext = row[2]
            old_count_ref = row[3]
            old_count_cover = row[4]
            break

        if not now_count_metadata:
            now_count_metadata = "0"
        if not old_count_metadata:
            old_count_metadata = "0"

        if not now_count_fulltext:
            now_count_fulltext = "0"
        if not old_count_fulltext:
            old_count_fulltext = "0"

        if not now_count_ref:
            now_count_ref = "0"
        if not old_count_ref:
            old_count_ref = "0"

        if not now_count_cover:
            now_count_cover = "0"
        if not old_count_cover:
            old_count_cover = "0"

        count_metadatas = int(now_count_metadata) - int(old_count_metadata)
        count_fulltexts = int(now_count_fulltext) - int(old_count_fulltext)
        count_refs = int(now_count_ref) - int(old_count_ref)
        count_covers = int(now_count_cover) - int(old_count_cover)
        self.inputobj2.add((provider, provider_popular,
                            int(old_count_metadata), int(now_count_metadata), count_metadatas,
                            int(old_count_fulltext), int(now_count_fulltext), count_fulltexts,
                            int(old_count_ref), int(now_count_ref), count_refs,
                            int(old_count_cover), int(now_count_cover), count_covers))
        # sql = "replace into result (`provider`,`counts`) values ('{}',{}) ".format(provider, counts)
        # self.mysqlutiles.ExeSqlToDB(sql)
        # else:
        #     self.inputobj2.add((provider, 0))
        # sql = "replace into result (`provider`,`counts`) values ('{}',{}) ".format(provider, -1)
        # self.mysqlutiles.ExeSqlToDB(sql)


if __name__ == "__main__":
    count = CountGrowth()
    count.set_config_file(r"D:\xjlib-common\logicaltool\sqltools\count_year_growth\config.toml")
    count.read_config()
    count.conn_mysql(builder="MysqlBuilderForToml")
    # provider
    # srv_product 类型 比如 智图
    # provider_popular 任务中文描述
    sql = "select provider,srv_product,provider_popular from task where if_active=1"
    count.set_sql(sql)
    rows = count.mysql_select()
    count.deal_mysql_select(rows)

    for provider, provider_popular in count.inputobj1:
        count.select_count(provider, count.dic['file']['EndDate'], count.dic['file']['StartDate'], provider_popular)

    xlsx_utils = OpenpyxlUtils()
    xlsx_utils.create_ws(count.dic['file']['SheetName'])
    xlsx_utils.set_ws_value("A1", "provider")
    xlsx_utils.set_ws_value("B1", "provider_popular")
    xlsx_utils.set_ws_value("C1", count.dic['file']['StartDate'])
    xlsx_utils.set_ws_value("D1", count.dic['file']['EndDate'])
    xlsx_utils.set_ws_value("E1", "题录")
    xlsx_utils.set_ws_value("F1", count.dic['file']['StartDate'])
    xlsx_utils.set_ws_value("G1", count.dic['file']['EndDate'])
    xlsx_utils.set_ws_value("H1", "全文")
    xlsx_utils.set_ws_value("I1", count.dic['file']['StartDate'])
    xlsx_utils.set_ws_value("J1", count.dic['file']['EndDate'])
    xlsx_utils.set_ws_value("K1", "引文")
    xlsx_utils.set_ws_value("L1", count.dic['file']['StartDate'])
    xlsx_utils.set_ws_value("M1", count.dic['file']['EndDate'])
    xlsx_utils.set_ws_value("N1", "封面")

    al = xlsx_utils.get_Alignment()
    font = xlsx_utils.get_font()
    xlsx_utils.set_font(font, al, ["A1", "B1", "C1", "D1", "E1", "F1", "G1", "H1", "I1", "J1", "K1", "L1", "M1", "N1"])

    listvalue = list(count.inputobj2)
    for i in range(2, len(listvalue) + 2):
        xlsx_utils.set_ws_value("A{}".format(i), listvalue[i - 2][0])
        xlsx_utils.set_ws_value("B{}".format(i), listvalue[i - 2][1])
        xlsx_utils.set_ws_value("C{}".format(i), listvalue[i - 2][2])
        xlsx_utils.set_ws_value("D{}".format(i), listvalue[i - 2][3])
        xlsx_utils.set_ws_value("E{}".format(i), listvalue[i - 2][4])
        xlsx_utils.set_ws_value("F{}".format(i), listvalue[i - 2][5])
        xlsx_utils.set_ws_value("G{}".format(i), listvalue[i - 2][6])
        xlsx_utils.set_ws_value("H{}".format(i), listvalue[i - 2][7])
        xlsx_utils.set_ws_value("I{}".format(i), listvalue[i - 2][8])
        xlsx_utils.set_ws_value("J{}".format(i), listvalue[i - 2][9])
        xlsx_utils.set_ws_value("K{}".format(i), listvalue[i - 2][10])
        xlsx_utils.set_ws_value("L{}".format(i), listvalue[i - 2][11])
        xlsx_utils.set_ws_value("M{}".format(i), listvalue[i - 2][12])
        xlsx_utils.set_ws_value("N{}".format(i), listvalue[i - 2][13])
    xlsx_utils.save_excel(count.dic['file']['OutFile'])
